<?php
// +----------------------------------------------------------------------
// | Quotes [未来可期]
// +----------------------------------------------------------------------
// +----------------------------------------------------------------------
// | Author: 夸克广告自助系统 https://support.qq.com/products/129921/ 作者QQ：79517721
// +----------------------------------------------------------------------
// | Date: 2022年3月5日
// +----------------------------------------------------------------------


function responseResult($code,$msg = "操作成功", $data="",$count = 0){
    $result = array('code'=>$code,'msg'=>$msg,'data'=>$data,'count'=>$count);
    exit(json_encode($result));
}

if (!function_exists('getIp')) {
    /**
     * author: 79517721@qq.com
     * time:2020/2/12 22:36
     * description:TODO 获取IP
     * @return mixed
     */
    function getIp()
    {
        $ip = $_SERVER['REMOTE_ADDR'];
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
            foreach ($matches[0] AS $xip) {
                if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
                    $ip = $xip;
                    break;
                }
            }
        } elseif (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (isset($_SERVER['HTTP_CF_CONNECTING_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CF_CONNECTING_IP'])) {
            $ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
        } elseif (isset($_SERVER['HTTP_X_REAL_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_X_REAL_IP'])) {
            $ip = $_SERVER['HTTP_X_REAL_IP'];
        }
        return $ip;
    }
}

if (!function_exists('daddslashes')) {

    /**
     * author: 79517721@qq.com
     * time:2020/2/12 22:36
     * description:TODO 过滤
     * @param $string
     * @param int $force
     * @param bool $strip
     * @return array|string
     */
    function daddslashes($string, $force = 0, $strip = FALSE)
    {
        !defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
        if (!MAGIC_QUOTES_GPC || $force) {
            if (is_array($string)) {
                foreach ($string as $key => $val) {
                    $string[$key] = daddslashes($val, $force, $strip);
                }
            } else {
                $string = addslashes($strip ? stripslashes($string) : $string);
            }
        }
        return $string;
    }
}


function isQ($param = ""){
    if(empty($param)){
        $data = file_get_contents("php://input");
        $data = json_decode($data,true);
    }else{
        $data = $param;
    }
    return $data;
}

/**
 * author: 79517721@qq.com
 * time:2020/2/12 22:36
 * description:TODO 访问URL
 * @param $url
 * @return bool|string
 */
function curlGet($url)
{
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; U; Android 4.4.1; zh-cn; R815T Build/JOP40D) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 MQQBrowser/4.5 Mobile Safari/533.1');
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    $content = curl_exec($ch);
    curl_close($ch);
    return ($content);
}

/**
 * author: 79517721@qq.com
 * time:2020/2/12 22:36
 * description:TODO 校验是否存在
 * @param $string
 * @param $find
 * @return bool
 */
function strexists($string, $find)
{
    return !(strpos($string, $find) === FALSE);
}

function dstrpos($string, $arr)
{
    if (empty($string)) return false;
    foreach ((array)$arr as $v) {
        if (strpos($string, $v) !== false) {
            return true;
        }
    }
    return false;
}



function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0)
{
    $ckey_length = 4;
    $key = md5($key ? $key : ENCRYPT_KEY);
    $keya = md5(substr($key, 0, 16));
    $keyb = md5(substr($key, 16, 16));
    $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : '';
    $cryptkey = $keya . md5($keya . $keyc);
    $key_length = strlen($cryptkey);
    $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0) . substr(md5($string . $keyb), 0, 16) . $string;
    $string_length = strlen($string);
    $result = '';
    $box = range(0, 255);
    $rndkey = array();
    for ($i = 0; $i <= 255; $i++) {
        $rndkey[$i] = ord($cryptkey[$i % $key_length]);
    }
    for ($j = $i = 0; $i < 256; $i++) {
        $j = ($j + $box[$i] + $rndkey[$i]) % 256;
        $tmp = $box[$i];
        $box[$i] = $box[$j];
        $box[$j] = $tmp;
    }
    for ($a = $j = $i = 0; $i < $string_length; $i++) {
        $a = ($a + 1) % 256;
        $j = ($j + $box[$a]) % 256;
        $tmp = $box[$a];
        $box[$a] = $box[$j];
        $box[$j] = $tmp;
        $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
    }
    if ($operation == 'DECODE') {
        if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {
            return substr($result, 26);
        } else {
            return '';
        }
    } else {
        return $keyc . str_replace('=', '', base64_encode($result));
    }
}


/**
 * author: 79517721@qq.com
 * time:2020/1/2 20:59
 * description:TODO 发送邮件
 * @param $to
 * @param $title
 * @param $html
 * @param $config
 * @return bool|string
 */
function sendEmail($to, $sub, $msg)
{
    global $config;
    if ($config['mail_cloud'] == 1) {
        $url = 'http://api.sendcloud.net/apiv2/mail/send';
        $data = array(
            'apiUser' => $config['mail_apiuser'],
            'apiKey' => $config['mail_apikey'],
            'from' => $config['mail_name'],
            'fromName' => $config['site_name'],
            'to' => $to,
            'subject' => $sub,
            'html' => $msg);
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $json = curl_exec($ch);
        curl_close($ch);
        $arr = json_decode($json, true);
        if ($arr['statusCode'] == 200) {
            return true;
        } else {
            return implode("\n", $arr['message']);
        }
    } else {
        if (!function_exists("openssl_sign") && $config['mail_port'] == 465) {
            $mail_api = 'http://1.mail.qqzzz.net/';
        }
        if ($mail_api) {
            $post['sendto'] = $to;
            $post['title'] = $sub;
            $post['content'] = $msg;
            $post['user'] = $config['mail_name'];
            $post['pwd'] = $config['mail_pwd'];
            $post['nick'] = $config['site_name'];
            $post['host'] = $config['mail_smtp'];
            $post['port'] = $config['mail_port'];
            $post['ssl'] = $config['mail_port'] == 465 ? 1 : 0;
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $mail_api);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $ret = curl_exec($ch);
            curl_close($ch);
            if ($ret == '1') return true;
            else return $ret;
        } else {
            include_once ROOT . 'app/smtp.class.php';
            $From = $config['mail_name'];
            $Host = $config['mail_smtp'];
            $Port = $config['mail_port'];
            $SMTPAuth = 1;
            $Username = $config['mail_name'];
            $Password = $config['mail_pwd'];
            $Nickname = $config['site_name'];
            $SSL = $config['mail_port'] == 465 ? 1 : 0;
            $mail = new SMTP($Host, $Port, $SMTPAuth, $Username, $Password, $SSL);
            $mail->att = array();
            if ($mail->send($to, $From, $sub, $msg, $Nickname)) {
                return true;
            } else {
                return $mail->log;
            }
        }
    }
}



function sendPost($url, $post_data = array())
{
    ini_set('user_agent', 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; GreenBrowser)');
    $postdata = http_build_query($post_data);
    $options = array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Content-type:application/x-www-form-urlencoded',
            'content' => $postdata,
            'timeout' => 15 * 60 // 超时时间（单位:s）
        )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($url, false, $context);
    return $result;
}


/**
 * @param $data 需要加密的字符串
 * @param $key  加密的密码
 * @return string  加密后的字符串
 */
if (!function_exists('_encrypt')) {
    function _encrypt($data, $key)
    {
        $key = md5($key);
        $x = 0;
        $len = strlen($data);
        $l = strlen($key);
        $char = '';
        $str = '';
        for ($i = 0; $i < $len; $i++) {
            if ($x == $l) {
                $x = 0;
            }
            $char .= $key{$x};
            $x++;
        }
        for ($i = 0; $i < $len; $i++) {
            $str .= chr(ord($data{$i}) + (ord($char{$i})) % 256);
        }
        return base64_encode($str);
    }

}


function getAdName($data){
    switch ($data){
        case 'big':
            $title = "大横幅广告位";
            break;
        case 'small':
            $title = "小横幅广告位";
            break;
        case 'word':
            $title = "文字广告位";
            break;
        default:
            $title = "";
            break;
    }
    return $title;
}


/**
 * @param $data  加密的字符串
 * @param $key   解密密码
 * @return string 解密后的字符串
 */
if (!function_exists('_decrypt')) {
    function _decrypt($data, $key)
    {
        $key = md5($key);
        $x = 0;
        $data = base64_decode($data);
        $len = strlen($data);
        $l = strlen($key);
        $char = '';
        $str = '';
        for ($i = 0; $i < $len; $i++) {
            if ($x == $l) {
                $x = 0;
            }
            $char .= substr($key, $x, 1);
            $x++;
        }
        for ($i = 0; $i < $len; $i++) {
            if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) {
                $str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
            } else {
                $str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
            }
        }
        return $str;
    }
}


/**
 * author: 79517721@qq.com
 * time:2020/2/12 22:20
 * description:TODO 访问URL
 * @param $url
 * @param int $post
 * @param int $referer
 * @param int $cookie
 * @param int $header
 * @param int $ua
 * @param int $nobaody
 * @return bool|string
 */
function get_curl($url, $post = 0, $referer = 0, $cookie = 0, $header = 0, $ua = 0, $nobaody = 0)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $httpheader[] = "Accept: */*";
    $httpheader[] = "Accept-Encoding: gzip,deflate,sdch";
    $httpheader[] = "Accept-Language: zh-CN,zh;q=0.8";
    $httpheader[] = "Connection: close";
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    if ($post) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    }
    curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheader);
    if ($header) {
        curl_setopt($ch, CURLOPT_HEADER, TRUE);
    }
    if ($cookie) {
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
    }
    if ($referer) {
        if ($referer == 1) {
            curl_setopt($ch, CURLOPT_REFERER, 'http://m.qzone.com/infocenter?g_f=');
        } else {
            curl_setopt($ch, CURLOPT_REFERER, $referer);
        }
    }
    if ($ua) {
        curl_setopt($ch, CURLOPT_USERAGENT, $ua);
    } else {
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36');
    }
    if ($nobaody) {
        curl_setopt($ch, CURLOPT_NOBODY, 1);
    }
    curl_setopt($ch, CURLOPT_ENCODING, "gzip");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $ret = curl_exec($ch);
    curl_close($ch);
    return $ret;
}





?>